﻿using FalconSystemCatalogWebsite.ClassManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FalconSystemCatalogWebsite.Pages
{
    public partial class StockByCategories : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {                
                //Menu
                BuildStockCategoriesTree();

                //Stock List
                StockList();
            }
        }

        private void BuildStockCategoriesTree()
        {
            using (var Provider = new FalconSystemModel.FalconSystemProvider())
            {
                SessionManager sessionManager = (SessionManager)HttpContext.Current.Session["SessionManager"];                

                var categoriesByCompanyQuery = from cmy in Provider.StockCategoriesByCompanies
                                               join cat in Provider.StockCategories
                                               on cmy.StockCategoryID equals cat.StockCategoryID
                                               where cmy.CompanyID == sessionManager.CompanyID
                                               select new
                                               {
                                                   cat.StockCategoryID,
                                                   cat.Name,
                                                   cat.ImageUrl,
                                                   cat.ParentStockCategoryID
                                               };

                CategoriesRadTreeView.DataTextField = "Name";
                CategoriesRadTreeView.DataValueField = "ImageUrl";
                CategoriesRadTreeView.DataFieldID = "StockCategoryID";
                CategoriesRadTreeView.DataFieldParentID = "ParentStockCategoryID";

                CategoriesRadTreeView.DataSource = categoriesByCompanyQuery.ToList();
                CategoriesRadTreeView.DataBind();

            }
        }

        private void StockList()
        {
            using (var Provider = new FalconSystemModel.FalconSystemProvider())
            {
                SessionManager sessionManager = (SessionManager)HttpContext.Current.Session["SessionManager"];                
                var stockEntities = Provider.Stocks.Where(w => w.CompanyID == sessionManager.CompanyID).Take(30);

                StockRadListView.DataSource = stockEntities;
                StockRadListView.DataBind();

            }
        }

        public byte[] GetStockImage(object stockId)
        {
            SessionManager sessionManager = (SessionManager)HttpContext.Current.Session["SessionManager"];

            FalconSystemCatalogWebsite.StockFilesManagerService.StockFilesManagerServiceClient stockFilesManagerServiceClient = new StockFilesManagerService.StockFilesManagerServiceClient();
            byte[] imageByte = stockFilesManagerServiceClient.GetStockImage(sessionManager.CompanyID, Convert.ToInt32(stockId));

            return imageByte;
        }
    }
}